package com.chebaohui.iqs.service.impl.deal.pingan.login;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.CloseableHttpClient;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.service.impl.deal.pingan.util.HttpClientUtil;

public class Verification {
//	public static void main(String[] args) {
//		try {
//			Map<String, String> header=new HashMap<>();
//			header.put("Cookie", "BIGipServerICORE-PNBS_DMZ_PrdPool=2569346220.42357.0000;"
//					+ "CAS_SSO_COOKIE=9e90f8465e5298ac015e7041fa150006-20299a3702c542d8a8ab623cfc565a76;"
//					+ "JSESSIONID=wOcGk_7htrTQXCtZqX6AJipKv28Nl-j0E-qV2YFDAsK_f72Ihu5z!1467869233;"
//					+ "_WL_AUTHCOOKIE_JSESSIONID=b4TuzyBT15GVSnzsyjDD");
//			header.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
//			header.put("Accept-Encoding", "gzip, deflate, br");
//			header.put("Accept-Language", "zh-CN,zh;q=0.9");
//			header.put("Connection", "keep-alive");
//			header.put("Host", "icorepnbs.pingan.com.cn");
//			header.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
//			header.put("Referer", "https://icorepnbs.pingan.com.cn/icore_pnbs/templates/newQuotation.ftl?applicantPersonFlag=1&_v=1.68.3.54333&_v=1.68.3.54333");
//			header.put("Upgrade-Insecure-Requests", "1");
////			header.put("", "");
//			System.out.println(getOrderNo(header));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(Verification.class);
	
	public static Map<String, String> getLogin(){
		Map<String, String> login=new HashMap<String, String>();
		CloseableHttpClient httpClient = null;
		HttpClientContext httpContext = null;
		try {
			httpClient = HttpClientUtil.createSSLClientDefault();
			httpContext = HttpClientContext.create();
			
			JSONObject pa_l=LoginNew.login(httpClient, httpContext);
			log.info("平安系统登录返回:"+pa_l.toJSONString());
			if(!pa_l.getBooleanValue("state")){
				login.put("state", "false");
				login.put("msg", "平安登录失败...");
				return login;
			}
			
			//login.put("orderNo", getOrderNo(httpClient, httpContext));
			List<Cookie> lc = httpContext.getCookieStore().getCookies();
			JSONObject cookie=new JSONObject();
			for (int i = 0; i < lc.size(); i++) {
				cookie.put(lc.get(i).getName(), lc.get(i).getValue());
			}
			login.put("cookie", cookie.toJSONString());
			login.put("state", "true");
			login.put("msg", "平安登录成功--并获取登录标识成功...");
			return login;
		}catch (Exception e) {
			login.put("state", "false");
			login.put("msg", "平安登录错误:"+e.getMessage());
			return login;
		} finally {
			try {
				if (null != httpClient)
					httpClient.close();
			} catch (Exception e2) {
				log.error("平安获取登录标识<关闭>出现异常:" + e2);
			}
		}
	}
	
	/**
	 * 获取平安登录验证号
	 * 
	 * @return
	 */
	private static String getOrderNo(Map<String, String> header)throws Exception {
		try {
			String url="https://icorepnbs.pingan.com.cn/icore_pnbs/mainCtrl.tpl?"
					+ "applicantPersonFlag=1&familyPrd=&bsDetailCode=2-3-G-F&usageAttributeCode=02"
					+ "&ownershipAttributeCode=03&insuranceType=1&agentSalerName=&businessCertificateNum=&"
					+ "empBusinessCertificateNum=&deptCodeText=21825&secondLevelDepartmentCode=218"
					+ "&deptCode=21825&employeeCodeText=2180001299&employeeCode=2180001299&channelCode=G&"
					+ "agentCode=18000290&partnerWorknetPanel=218101408008&productCombineList=&worknetCode=&"
					+ "conferVal=1800029014002+8&agentNameLike=&agentCodeText=&brokerCode=&"
					+ "agentName=%E9%87%8D%E5%BA%86%E5%8D%8E%E7%91%9E%E4%BF%9D%E9%99%A9%E4%BB%A3%E7%90%86%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8&"
					+ "conferNo=1800029014002&subConferNo=8&dealerCode=&autoInsurance=true&propertyInsurance=false&accidentInsurance=false&"
					+ "rateClassFlag=14&employeeName=%E6%A1%82%E5%9E%9A%E6%9B%A6&saleGroupCode=21825000349&trafficProductCode=MP01000002&"
					+ "commercialProductCode=MP01000001&businessMode=undefined&systemId=ICORE-PTS&applyApproach=";

			String re = PAHttpUtil.GET(header, url);
			
			System.out.println(re);
			String token = "mainQuotationNo = '(.*?)';";
			Pattern p = Pattern.compile(token);
			Matcher m = p.matcher(re);
			while (m.find()) {
				String orderNo = m.group(1);
//				log.info("第" + (loginNum - 1) + "次登录平安,第" + orderNum + "次,获取验证号成功:" + orderNo);
//				orderNum++;
				return orderNo;
			}
//			log.error("第" + (loginNum - 1) + "次登录平安,第" + orderNum + "次,获取验证号失败...");
//			login = false;
			throw new Exception("无法获取得到标识");
		} catch (Exception e) {
//			login = false;
//			log.error("第" + (loginNum - 1) + "次登录平安,第" + orderNum + "次,获取验证号异常:" + e);
			throw new Exception("平安获取登录标识异常:"+e.getMessage());
		}
	}

}
